package weather.util.dictionary;

import java.io.*;
import java.util.*;

import org.apache.poi.ss.usermodel.*;

public abstract class AbstractEquivalenceCheckingDictionary<Entry> extends AbstractDictionary
{
	public void loadFromXlsFile(File path, int sheetNumber) throws IOException
	{
		Workbook wb = openWorkbook(path);
		Sheet sheet = wb.getSheetAt(sheetNumber);
		
		int firstCellNum = sheet.getRow(sheet.getFirstRowNum()).getFirstCellNum();
		
		for (int r = sheet.getFirstRowNum(); r <= sheet.getLastRowNum(); ++r)
		{
			Row row = sheet.getRow(r);
			
			for (int c = firstCellNum; c < row.getLastCellNum(); ++c)
			{
				String cellVal = row.getCell(c).getStringCellValue();
				if (cellVal.equals(""))
					break;
				entries.put(newEntry(cellVal), r);
			}
		}
	}

	public boolean areEntriesEquivalent(Entry left, Entry right)
	{
		return entries.get(left) == entries.get(right);
	}
	
	public boolean containsEntry(Entry entry)
	{
		return entries.containsKey(entry); 
	}
	
	public Set<Entry> getEntries()
	{
		return entries.keySet();
	}
	
	public Set<Entry> getEntriesEquivalentToEntry(Entry e)
	{
		Set<Entry> eqEntries = new HashSet<Entry>();
		
		for (Entry entry: entries.keySet())
			if (!entry.equals(e) && areEntriesEquivalent(entry, e))
				eqEntries.add(entry);
		
		return eqEntries;
	}
	
	protected abstract Entry newEntry(String s);
	
	private Map<Entry, Integer> entries = new HashMap<Entry, Integer>();
}
